package com.suray.system.run.data.mysql.analyze;

import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.system.run.core.LowBatteryDescription;
import com.suray.system.run.data.mysql.device.battery.LowBatteryDBServiceImpl;
import com.suray.system.run.data.mysql.device.entity.LowBatteryDB;
import com.suray.system.run.data.mysql.device.entity.OffLineDesDB;
import com.suray.system.run.type.DeviceType;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LowBatteryDataStore {
    private LowBatteryDBServiceImpl lowBatteryDBService = SpringContextHolder.getBean(LowBatteryDBServiceImpl.class);

    /**
     * 设备序列号
     */
    private String series;

    /**
     * 当前任务表
     */
    private LowBatteryDB lowBatteryDB;

    /**
     * 设备类型
     */
    private DeviceType deviceType;

    public LowBatteryDataStore(String series, DeviceType deviceType) {
        this.series = series;
        this.deviceType = deviceType;
    }

    /**
     * 查找当前的低电量信息
     * @return
     */
    public LowBatteryDB findLowBatteryData() {
        lowBatteryDB = lowBatteryDBService.findLowBatteryDBBySeries(series);
        return lowBatteryDB;
    }

    /**
     * 创建当前小车低电量信息
     * @return
     */
    public LowBatteryDB createLowBatteryData() {
        lowBatteryDB = new LowBatteryDB();
        lowBatteryDB.setSeries(series);
        lowBatteryDB.setDeviceType(deviceType);
        lowBatteryDB.setLowTime(new Date(System.currentTimeMillis()));
        lowBatteryDBService.save(lowBatteryDB);
        return lowBatteryDB;
    }

    /**
     * 更新当前小车低电量信息
     * @return
     */
    public boolean updateLowBatteryData() {
        return lowBatteryDBService.updateById(lowBatteryDB);
    }

    /**
     * 获取小车所有低电量信息
     * @param startTime
     * @param endTime
     * @return
     */
    public List<LowBatteryDB> getLowBatteryInfoList(Date startTime, Date endTime) {
        return lowBatteryDBService.getLowBatteryInfoList(series, startTime, endTime);
    }

    /**
     * 获取小车低电量次数
     * @return
     */
    public int getLowBatteryNum() {
        return lowBatteryDBService.count();
    }
}
